package com.netcracker.util.service;

import com.netcracker.db.exception.DBException;
import com.netcracker.entity.User;
import com.netcracker.util.dao.DAOFacade;
import com.netcracker.util.service.exception.DataTransmittingException;
import com.netcracker.util.service.exception.NullDataException;
import java.util.List;

public class AdminService {
    
    private DAOFacade daoFacade;
    
    public AdminService(DAOFacade daoFacade) {
        this.daoFacade = daoFacade;
    }
    
    public void setDAOFacade(DAOFacade daoFacade) {
        this.daoFacade = daoFacade;
    }

    public void deleteUser(int userId) throws DataTransmittingException {
        try {
            daoFacade.getUserDAO().remove(userId);
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
    }

    public List<User> findAllUsers() throws DataTransmittingException, NullDataException {
        List<User> users = null;
        try {
            users = daoFacade.getUserDAO().findAll();
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
        if (users == null)
        {
            throw new NullDataException("No one user was found");
        }
        return users;
    }

    public User findUserByLogin(String login) throws DataTransmittingException, NullDataException {
        User user = null;
        try {
            user = daoFacade.getUserDAO().findByLogin(login);
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
        if (user == null)
        {
            throw new NullDataException("User was not found");
        }
        return user;
    }

    public int countUsers() throws DataTransmittingException {
        int userCount = 0;
        try {
            userCount = daoFacade.getUserDAO().count();
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
        return userCount;
    }
    
}